package gui;

import java.io.BufferedWriter;
import java.io.IOException;

import constants.Cmd;
import constants.Rights;

public class GUIControl{
	private GUI gui;
	private BufferedWriter bw;
	public GUIControl(String accessRights, BufferedWriter bw){
		this.bw=bw;
		gui = new GUI(this);
		if(accessRights.equals(Rights.doctorAccess)){
			gui.setView("DoctorView");
		}else if(accessRights.equals(Rights.nurseAccess)){
			gui.setView("NurseView");
		}else if(accessRights.equals(Rights.agencyAccess)){
			gui.setView("AgencyView");
		}else if(accessRights.equals(Rights.patientAccess)){
			gui.setView("PatientView");
		}else{
			gui.setView("Error");
		}
	}
	public void sendToServer(String message){
		System.out.println("Client skickar: "+message);
		try {
			bw.write(message+"\n");
			bw.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void messageFromServer(String message){ //klart
		System.out.println("From server: "+message);
		if(message.startsWith(Cmd.error)){
			gui.setView("Error");
			gui.pack();
		}else if(message.startsWith(Cmd.success)){
			gui.setView("Success");
			gui.pack();
		}else if(message.startsWith(Cmd.readRecord)){
			System.out.println(message);
			gui.readRecordFrame.showRecord(message.substring(Cmd.newRecord.length()+1));
			gui.setView("Success");
			gui.pack();
		}
	}
	//l�ses genom att skicka Cmd.Read+subjectName
	public void readPatientsJournal() {
		sendToServer(Cmd.readSubjectsRecord);
	}
}
